/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.bis.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.Area;
import com.jeesite.modules.sys.utils.AreaUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.bis.entity.BisUser;
import com.jeesite.modules.bis.service.BisUserService;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * bis_userController
 * @author Zhangning
 * @version 2019-08-22
 */
@Controller
@RequestMapping(value = "${adminPath}/bis/bisUser")
public class BisUserController extends BaseController {

	@Autowired
	private BisUserService bisUserService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public BisUser get(String id, boolean isNewRecord) {
		return bisUserService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("bis:bisUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(BisUser bisUser, Model model) {
		model.addAttribute("bisUser", bisUser);
		return "modules/bis/bisUserList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("bis:bisUser:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<BisUser> listData(BisUser bisUser, HttpServletRequest request, HttpServletResponse response) {
		bisUser.setPage(new Page<>(request, response));
		bisUser.setStatus("0");
		Page<BisUser> page = bisUserService.findPage(bisUser);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("bis:bisUser:view")
	@RequestMapping(value = "form")
	public String form(BisUser bisUser, Model model) {
		model.addAttribute("bisUser", bisUser);
		return "modules/bis/bisUserForm";
	}

	/**
	 * 保存bis_user
	 */
	@RequiresPermissions("bis:bisUser:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated BisUser bisUser) {
		bisUserService.save(bisUser);
		return renderResult(Global.TRUE, text("保存微信用户成功！"));
	}
	
	/**
	 * 删除bis_user
	 */
	@RequiresPermissions("bis:bisUser:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(BisUser bisUser) {
//		bisUserService.delete(bisUser);
		bisUser.setStatus("1");
		bisUserService.updateStatus(bisUser);
		return renderResult(Global.TRUE, text("删除微信用户成功！"));
	}

	@RequiresPermissions("bis:bisUser:edit")
	@RequestMapping({"treeData"})
	@ResponseBody
	public List<Map<String, Object>> treeData() {
		List<Map<String, Object>> mapList = ListUtils.newArrayList();
		List<BisUser> list = bisUserService.findList(new BisUser());
		list.forEach(bisUser -> {
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", bisUser.getId());
			map.put("pId", "0");
			map.put("name", bisUser.getName() + " " + bisUser.getMobilePhone());
			map.put("isParent", false);
			mapList.add(map);
		});
		return mapList;
	}
	
}